package com.dotflat.xj.service.impl;

import com.dotflat.check.domain.Card;
import com.dotflat.check.repository.CardRepository;
import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import com.dotflat.xj.domain.TargetClass;
import com.dotflat.xj.repository.TargetClassRepository;
import com.dotflat.xj.service.TargetClassService;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.utils.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;


@Service
public class TargetClassServiceImpl extends AreableObjectBaseServiceImpl<TargetClass, TargetClassRepository> implements TargetClassService {


    @Autowired
    @Override
    public void setBaseRepository(TargetClassRepository repository) {
        super.setBaseRepository(repository);
    }

    @Autowired
    private TargetClassRepository targetClassRepository;

    @Autowired
    private CardRepository cardRepository;

    @Override
    public TargetClass update(TargetClass targetClass) {
        return targetClassRepository.save(targetClass);
    }

    @Override
    public TargetClass update(String id, String cardCode) {

        Card card = cardRepository.findByCardNo(cardCode);
        if (Objects.isNull(card)) {
            throw new BusinessException(ErrorCode.IS_TARGET,cardCode+ "卡号未导入！");
        }
        card.setStatus(1);
        card.setPurposeType("巡检");
        card.setType(1);
        cardRepository.save(card);
        TargetClass targetClass1 = targetClassRepository.findByCardCode(cardCode);
        if (Objects.nonNull(targetClass1)) {
            throw new BusinessException(ErrorCode.IS_TARGET, cardCode+"卡号已绑定！");
        }

        TargetClass targetClass = targetClassRepository.getOne(id);
        if (Objects.nonNull(targetClass.getId())) {
            targetClass.setCardCode(cardCode);
            targetClass.setCardTime(new Date());
            targetClassRepository.save(targetClass);
        } else {
            throw new BusinessException(ErrorCode.IS_TARGET, "对象不存在！");
        }
        return targetClass;
    }

    @Override
    public void cardCodeUnbound(String cardCode) {
        TargetClass targetClass1 = targetClassRepository.findByCardCode(cardCode);
        if (Objects.nonNull(targetClass1)) {
            targetClass1.setCardCode("");
            targetClassRepository.save(targetClass1);
        } else {
            throw new BusinessException(ErrorCode.IS_TARGET, "卡号不存在！");
        }
    }

    @Override
    @Transactional
    public void copyCommon(StaffDTO staffDTO) {
        List<TargetClass> list =  targetClassRepository.findByAreaId("0");
        TargetClass targetClass;
        for (int i = 0; i < list.size(); i++) {
            targetClass = new TargetClass();
            targetClass.setName(list.get(i).getName());
            targetClass.setType(list.get(i).getType());
            targetClass.setAreaId(staffDTO.getAreaId());
            targetClass.setCreatedBy(staffDTO.getId());
            TargetClass targetClass1 = targetClassRepository.findByAreaIdAndName(staffDTO.getAreaId(),list.get(i).getName());
            //园区巡检地址不存在在添加
            if (Objects.isNull(targetClass1)){
                targetClassRepository.save(targetClass);
            }
        }
    }
}